Chapter 3 : Operators In Python
From book Python Programming (Problem solving, Packages and Libraries) published by McGraw Hill Education (India) Private limited.
By:
Note the following:-
>>> symbol and therefore cannot be directly executed. If you want to execute them on IDLE or Jupyter, you need to manually remove the >>> symbol.Markdown and code cells (extension .ipynb) and then downloaded as html. If someone wants to "modify" or "extend' this document, you may ask for the original .ipynb file by sending me an e-mail at:- 999.anuraggupta@gmail.com3.2. Assignment (And reassignment)
The equal to sign (=) in Python is not the same as in traditional high school maths. The equal to sign (=) is an assignment operator, rather than a mathematical operator in Python.
For instance, if you have x=12, it means x has been assigned a value of 12. This is something like x ← 12. This means that the entity on the left hand side of the (=) sign has to be a variable. So you cannot have 12=x. You can’t even do 12 = 12 in Python because you cannot “assign” a value to 12, which in Python is a literal. If you try to do this, your IDLE will give the following response:
# ---ON IDLE---
>>>12 = x
SyntaxError: can't assign to literal
>>>12 = 12
SyntaxError: can't assign to literal
Note:- Space “around”, that is, on either side of an operator does not matter. But space is not allowed within the “two tokens” of an operator, such as ** and //. This is clear from the following:-
# ---ON IDLE---
>>>3 + 4 * 5#OK
23
>>>3 ** 2#OK
9
>>>3 * * 2#Not OK as space between * and * not allowed
SyntaxError: invalid syntax
Note:- In Python, the sign of the remainder is the same as the sign of the denominator (which differs from languages, such as C, where it is the same as the sign of the numerator). This is clear from following:
# ---ON IDLE---
>>>13 % 5# Denominator positive, so remainder positive
3
>>>13 % -5# Denominator negative, remainder negative
-2
>>> -13 % 5# Numerator negative, denominator positive, so remainder positive
2
Following example code in IDLE illustrates some outcomes of common operations:
# ---ON IDLE---
>>>2 * 3# Multiplying 2 integers gives integer
6
>>>2/3 # Division by integer gives float
0.6666666666666666
>>> type(2/3) # You can check the type of any object using type() function
<class'float'>
Note that the plus, that is, “+” operator can be used to “concatenate” two strings. So when you “add” strings using the “+” operator, you are not adding like numbers but rather you are concatenating. (Concatenation is the joining of two strings end-to-end.). For instance, adding 1 to 2 will give different results than adding ‘1’ to ‘2’, as shown:-
# ---ON IDLE---
>>>'1' + '2'# The + operator concatenates two strings
'12'
>>>1 + 2# The plus ie + operator does arithmetic addition on two integer
3
Further note that the multiplication operator, that is, “*” can be used on strings, lists and tuples as follows:-
# ---ON IDLE---
>>>'abc' * 3# OK. String can be multiplied with integer
'abcabcabc'
>>> [1,2,3]* 3# OK. List can be multiplied with integer
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> (1,2,3) * 3# OK. Tuple can be multiplied with integer
(1, 2, 3, 1, 2, 3, 1, 2, 3)
3.3.3. Comparison
Comparison operators in Python
There are six comparison operations in Python. They are used to compare two values.
All comparison operators return a value of either True or False according to the condition. Note that just like the + operator takes two values(operands) and returns an int value, the comparison operators also take two values (Operands) and return a Bool value.
For instance, consider the following script:-
# ---ON IDLE---
>>> a = (5>3)# 5> 3 evaluates to bool True
>>> a
True
>>> type(a)# variable a is of type bool
<class 'bool'>
Here are some examples of use of comparison operators:
# ---ON IDLE---
>>>1<2 #Evaluates to True
True
>>>2<=2 #Evaluates to True
True
>>>1>3
False
>>> a = 1
>>> a is1 # a and 1 are same objects hence True
True
>>> s = 'abcdef' # 'a' is in string 'abcdef', hence True
>>>'a' in s
True
>>> L = [1,2,3,4,5]
>>>6 not in L # 6 is not in L hence True
True
3.3.5. Bitwise Operators
The following are important concepts regarding bit-wise operators:
Bitwise operators: These act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.
Some common bit-wise operations on IDLE are shown as follows:
# ---ON IDLE---
# 6 is 00000110, 5 is 00000101
>>>6&5 # 00000110 & 00000110 is 00000100 (AND)
4
>>>6 ^ 5 # 00000110 ^ 00000101 is 00000011 (XOR)
3
>>>6 | 5 # 00000110 | 00000101 is 00000111 (OR)
7
>>>6<<2 # 00000110 shifted left by 2 bits is 00011000
24
>>>6>>2 # 00000110 shifted right by 2 bits is 000000001
1
>>> ~ 6 # 00000110 bit wise NOT is 11111001 in 2s compliment (-7 in decimal)
-7
3.3.8. Identity Operator
In Python “is” and “is not” are the identity operators.
The identity operators check if two variables “point” to the same location in memory.
Note that “equality” of two objects (Indicated by the “==” operator), does not imply they are identical. (Identicality is indicated by “is”.).
If two variables are equal, it does not necessarily imply that they are identical.
The following examples on IDLE clarifies the concept of “equality” versus identicality:
# ---ON IDLE---
>>> a = b = [1,2,3] # and b point to same object
>>> c = [1,2,3] # c is logically same as a and b but is a different object
>>>print('id(a) ', id(a), 'id(b) ', id(b), 'id(c) ', id(c))
id(a) 36929088 id(b) 36929088 id(c) 36487376# id of a and b same but of c
>>> a is b
True
>>> a is c # a and c are different objects
False
3.3.9. Membership Operator
Membership operators in Python are “in” and “not in”.
You can use the membership operators to check whether a value or variable is present in a collection or a sequence (string, list, tuple, set and dictionary).
Note that in a dictionary you can use the membership operators to only check for the presence of “keys”.
You cannot use the membership operators to check for presence of a “value” in a dictionary.
Here are some examples of use of membership operators on IDLE:-
# ---ON IDLE---
>>>'a' in 'abcd'# True since character ‘a’ is part of the string ‘abcd’
True
>>>5 in [7,6,5,4]# True since 5 is in the list
True
>>>4 in [9,0,7,8]#False since 4 is not in the list
False
>>>'a' in {'a' : 'apple', 'b' : 'boy', 'c' : 'cat'} #in can be used for key
True
>>>'apple' in {'a' : 'apple', 'b' : 'boy', 'c' : 'cat'} # Not for value
False